/**
 * 
 */
/***************************************************************
                           公告管理模块
***************************************************************/
var addNoticeDialog;
function noticeManagerInit()
{
	 $("#noticeTable tr:nth-child(even)").addClass("trevencolor");
	 BUI.use(['bui/overlay','bui/form'],function(Overlay,Form){
	      	var form = new Form.HForm({
	        	srcNode : '#form'
   	 }).render();
	 
	 addNoticeDialog = new Overlay.Dialog({
         title:'添加公告',
         width:800,
         height:450,
         //配置DOM容器的编号
         contentId:'notice_content',
         cancel:function(){
	      	choose_close();
	      	clearNoticeDialog();
	     },
         success:function () {
        	 
        	saveOrUpdateNotice();
        	
           }
       });
   });
	 
   $("#btnShow").bind("click",function(){
	   clearNoticeDialog();
	   addNoticeDialog.set("title","添加公告");
	   $("#type").val("1");
	   addNoticeDialog.show();
	   choose_open();
   });
   
   noticeValidate();
}

/**
 *	添加公告 type=1
 *  编辑公告 type=2
 */
function saveOrUpdateNotice()
{
	var type = $("#type").val();
	if(type == 1)
	{
		var title = $("#title").val();
		var createDate = $("#createDate").val();
		var memo = $("#memo").val();
		if($("#noticeForm").validate().form()){
			addNoticeDialog.close();
			var url = projectName + "/notice/save.do?date=" + new Date().getTime();
			alert(url);
			$.ajax({
				url: url,
				type: "post",
				async: false,
//				data: $('#noticeForm').serialize(),
				data:{
					title:title,
					createDateString:createDate,
					memo:memo,
				},
				success: function(data,result)
				{
					if("SUCCESS" == data)
					{
						messageAlert(1,"添加公告成功",function()
						{
							this.close();
							choose_close();
							showNoticePageDataList(1);
						});
					}
					else
					{
						messageAlert(1,"添加公告失败，请重试",function()
								{
							this.close();
							choose_close();
							showNoticePageDataList(1);
						});
					}
				}
			});
		}
	}
	
}

/**
 *	清空表单数据 
 */
function clearNoticeDialog(){
	noticeFormValidate.resetForm();
	$("#title").val("");
	$("#title").attr("readonly",false);
	$("#createDate").val("");
	$("#createDate").attr("disabled",false);
	$("#memo").val("");
	$("#memo").attr("readonly",false);
}

/**
 *	删除公告 
 */
function deleteNotice(id)
{
	$.ajax({
		 url: projectName+"/notice/deleteByIds.do?date=" + new Date().getTime(),
		 type: "post",
		 data: {"ids": id},
		 async: false,
		 success: function(data,result)
		 {
			 if("SUCCESS" == data){
				 messageAlert(1,"删除公告成功",function()
				 {
						this.close();
						choose_close();
						showNoticePageDataList(1);
				 });
			 }
			 else if("ERROR" == data)
			 {
				 messageAlert(3,"删除公告失败",function(){
						this.close();
						choose_close();
						showNoticePageDataList(1);
				 });
			 }
		 }
	 });
}

/**
 *	判断批量删除公告是否为空
 */
function del_notice(cbxName)
{
	var ids = getSelectCbx(cbxName);
	if(ids == "")
	{
		messageAlert(2,"请选择要删除的公告！",function(){choose_close(),this.close()});
	}
	else
	{
		messageAlert(4,'你确定要删除选择的公告吗？',function(){choose_open,deleteByIds(ids)},1)
	}
}

/**
 *	批量删除公告 
 */
function deleteByIds(ids){
	$.ajax({
		 url : projectName + "/notice/deleteByIds.do?date=" + new Date().getTime(),
		 type: "post",
		 data: {"ids": ids},
		 async: false,
		 success: function(data,result){
			 if("SUCCESS" == data){
				 messageAlert(1,"删除公告成功",function(){
						this.close();
						choose_close();
						showNoticePageDataList(1);
				 });
			 }
			 
			 else if("ERROR" == data)
			 {
				 messageAlert(3,"删除公告失败",function(){
						this.close();
						choose_close();
						showNoticePageDataList(1);
				 });
			 }
		 }
	 });
}

/**
 *	编辑公告 
 */
function noticeEdit(id)
{
	$.ajax({
		url : projectName+"/notice/findNoticeById.do?date=" + new Date().getTime(),
		async : false,
		data:{"id":id},
		success : function(data) {
			if(null != data&&data != "")
			{
				clearNoticeDialog();
				$("#title").val(data.title);
				$("#title").attr("readonly","readonly");
				var createDateString = new Date(data.createDate);
				$("#createDate").val(createDateString);
				$("#createDate").attr("disabled",true);
				$("#memo").val(data.memo);
				$("#memo").attr("readonly","readonly");
				$("#type").val("2");
				choose_open();
				addNoticeDialog.set("title","查看公告");
				addNoticeDialog.show();		
			}
			else
			{
				messageAlert(0,"该公告已被删除！",function(){
					choose_close(),
					this.close(),
					showNoticePageDataList(1)
					});
			}
		}
	});
	
}


/***************************************************************
							公告分页
***************************************************************/
/**
 *	公告分页对象 
 */
var noticePageInfo={
		orderBy:null,
		order:"desc",
		pageNo:"1",
};

/**
 * 获取公告名查询条件
 */
function getTitle()
{
	var title =  $("#search_title").val();
	if(undefined == title||null == title)
	{
		title = "";
	}
	return title;
}


/**
 * 查询公告数据
 */
function showNoticePageDataList(pageNo)
{
	noticePageInfo.pageNo = pageNo;
	var title = getTitle();
	var noticePageInfoString = JSON.stringify(noticePageInfo);
	window.location.href = projectName + "/notice/getPageNotice.do?PageInfo=" + noticePageInfoString + "&title=" + title;
}

/***************************************************************
							表单验证
***************************************************************/
var noticeFormValidate;
function noticeValidate()
{
	noticeFormValidate = $("#noticeForm").validate({
		errorPlacement: function(error, element) { 
         	error.appendTo(element.next());
         },
		rules:{
			title:
			{
				required:true,
			},
			createDate: 
			{
				required:true,
			},
			memo:
			{
				required:true,
			},
		},
		messages:{
			title:
			{
                required:"公告名不能为空"
            },
            createDate:
            {
            	required:"日期不能为空"
            },
            memo:
            {
                required:"内容不能为空",
            }                                
        }
		
	});

}
